﻿using System;
using System.Xml;
using System.IO;
using Fjaspx.Common;

namespace Fjaspx.UI.Web
{
    public partial class Administrator_FromXml : UserPage
    {
        /// <summary>
        /// 
        /// </summary>
        public string XMLpath = null;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string MexBoxContent = Fjaspx.Common.SystemCms.Read_File(Server.MapPath("~/administrator/SystemTemplate/MexBox.htm"), "utf-8");
                if (Request["admin"] == null)
                {

                     // XMLpath = Server.MapPath("~/App_Data/From/" + Request.UrlReferrer.ToString().Replace("http://", "").Replace("/", "_") + ".xml");
                    if (Request["name"] != null)
                    {
                        XMLpath = Server.MapPath("~/App_Data/From/" + Request["name"] + ".xml");
                    }
                    else {
                        MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                        MexBoxContent = MexBoxContent.Replace("{$message}", "没有文件标识name参数，提交信息失败！ ");
                        Response.Write(MexBoxContent);
                    }
                    if (XmlNew())
                    {
                        MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                        MexBoxContent = MexBoxContent.Replace("{$message}", "提交信息成功，感谢您的参与！ ");
                        Response.Write(MexBoxContent);
                    }
                    else
                    {
                        MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                        MexBoxContent = MexBoxContent.Replace("{$message}", "提交信息失败，请联系管理员！ ");
                        Response.Write(MexBoxContent);
                    }
                }
                else 
                {
                    //SystemCms.AdminSesion();
                    if (Request["name"] != null)
                    {
                        Response.ContentType = "application/xml"; // 输出并按xml数据显示
                        Response.Write(SystemCms.Read_File(Server.MapPath("~/App_Data/From/" + Request["name"] + ".xml"), ___WebConfig.templet_language));
                    }
                    else {
                        MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                        MexBoxContent = MexBoxContent.Replace("{$message}", "没有文件标识name参数，查看信息失败！ ");
                        Response.Write(MexBoxContent);
                    }
                }
            }
            
        }
        //
        /// <summary>
        /// XMLs the new.
        /// </summary>
        /// <returns></returns>
        bool XmlNew()
        {
            try
            {
                if (!File.Exists(XMLpath))
                {
                  return  NewXml();
                }
                else
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(XMLpath);
                    XmlElement root = doc.DocumentElement;
                    XmlNode node = doc.CreateElement("From");
                    XmlAttribute attr = null;
                    attr = doc.CreateAttribute("Time");
                    attr.Value = DateTime.Now.ToString();
                    node.Attributes.SetNamedItem(attr);

                    for (int i = 0; i < Request.Form.Count; i++)
                    {
                        ////带有"<![CDATA[   ]]>"的元素 
                        XmlElement element = doc.CreateElement("Field");
                        element.SetAttribute("Name", Request.Form.GetKey(i));
                        XmlCDataSection idata = doc.CreateCDataSection("item");
                        idata.InnerText = Request.Form.Get(i);
                        element.AppendChild(idata);
                        node.AppendChild(element);
                    }
                    root.AppendChild(node);
                    doc.Save(XMLpath);
                    Console.Write(doc.OuterXml);
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// News the XML.
        /// </summary>
        /// <returns></returns>
        bool NewXml()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", ___WebConfig.templet_language, null);
                doc.AppendChild(dec);
                //创建一个根节点（一级） 
                XmlElement root = doc.CreateElement("Fjaspx");
                doc.AppendChild(root);
                //创建节点（二级） 
                XmlNode node = doc.CreateElement("From");
                XmlAttribute attr = null;
                attr = doc.CreateAttribute("Time");
                attr.Value = DateTime.Now.ToString();
                node.Attributes.SetNamedItem(attr);
                for (int i = 0; i < Request.Form.Count; i++)
                {
                    //带有"<![CDATA[   ]]>"的元素 
                    XmlElement element = doc.CreateElement("Field");
                    element.SetAttribute("Name", Request.Form.GetKey(i));
                    ////element.InnerText = "Round Comment";
                    XmlCDataSection idata = doc.CreateCDataSection("item");
                    idata.InnerText = Request.Form.Get(i);
                    element.AppendChild(idata);
                    node.AppendChild(element);
                }
                root.AppendChild(node);
                doc.Save(XMLpath);
                Console.Write(doc.OuterXml);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}